import { DisplaySlotId, system, world } from "@minecraft/server";
import { GameComponent } from "@sapi-game/main";
import { ScoreBoardDataBase } from "sapi-pro";
import { CvSBackState } from "./backState";

export class CvSScoreManager extends GameComponent<CvSBackState> {
    private showDb = new ScoreBoardDataBase("CvS_team", "§a鸡王争霸-选队");
    private lastRefresh: number = 0;

    override onAttach(): void {
        this.showDb.resetAll();
    }

    refreshScoreBoard() {
        if (this.lastRefresh == system.currentTick) return;
        const scores: string[] = [];
        this.state.chicken.forEach((p) => {
            scores.push("§c[鸡]§r" + p.name);
        });
        this.state.shepherd.forEach((p) => {
            scores.push("§b[猎]§r" + p.name);
        });
        this.showDb.clear();
        this.showDb.setDisplaySlot(DisplaySlotId.Sidebar);
        for (let i = 0; i < scores.length; i++) {
            this.showDb.set(scores[i], i);
        }
        this.lastRefresh = system.currentTick;
    }

    override onDetach(): void {
        this.showDb.dispose();
    }
}
